package gitdemo1.git3.pojo;

import ch.qos.logback.classic.spi.EventArgUtil;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static java.lang.Thread.sleep;

public class MyRunable implements Runnable{

    private static int ticket=100;
    private Object obj=new Object();
    Lock lock=new ReentrantLock();
    @Override
    public void run() {
        while(true) {
            try {
                lock.lock();
                if (ticket > 0) {
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + ticket);
                    ticket--;
                }
            }finally {
                lock.unlock();
            }


        }
    }

    private synchronized void sellTicket() {

            if (ticket > 0) {
                try {
                    sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ":" + ticket);
                ticket--;
            }

    }
}
